package com.xx.ViewInfo;

import com.xx.hdfs.HdfsDemo;
import com.xx.hdfs.HdfsLog;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import org.apache.hadoop.fs.FileStatus;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class TextAreaView extends TextArea {
    HdfsDemo demo;
    FileStatus status;
    private Image TeditI;
    private Image TsaveI;
    private Image TAllI;
    private Image TcopyI;
    private Image TstickupI;
    private Image TcutI;
    private Image TquitI;
    private boolean isPreview = false;
    public StringBuilder old = new StringBuilder();
    private MenuItem edit;
    private MenuItem save;
    private MenuItem all;
    private MenuItem copy;
    private MenuItem stickup;
    private MenuItem cut;
    private MenuItem quit;
    private HdfsLog log;

    public TextAreaView(HdfsDemo demo, FileStatus status
            , Image TeditI, Image TsaveI, Image TAllI, Image TcopyI, Image TstickupI,
                        Image TquitI
            , Image TcutI, HdfsLog log) {
        this.log = log;
        this.TeditI = TeditI;
        this.TsaveI = TsaveI;
        this.TAllI = TAllI;
        this.TcopyI = TcopyI;
        this.status = status;
        this.TstickupI = TstickupI;
        this.TquitI = TquitI;
        this.TcutI = TcutI;
        this.demo = demo;
        initContextMenu();
    }

    public void setIsPreview(Boolean isPreview) {
        this.isPreview = isPreview;
        if (isPreview) {
            edit.setDisable(true);
            save.setDisable(true);
            stickup.setDisable(true);
            cut.setDisable(true);
            quit.setDisable(true);

        }
    }

    private void initContextMenu() {
        ContextMenu areaCm = new ContextMenu();

        edit = new MenuItem();
        editAction();
        HBox editHb = new HBox(10);
        ImageView editI = new ImageView(TeditI);
        editI.setPreserveRatio(true);
        editI.setFitWidth(25);
        Text editT = new Text("编辑");
        editHb.getChildren().addAll(editI, editT);
        edit.setGraphic(editHb);


        save = new MenuItem();
        HBox saveHb = new HBox(10);
        savaAction();
        ImageView saveI = new ImageView(TsaveI);
        saveI.setPreserveRatio(true);
        saveI.setFitWidth(25);
        Text saveT = new Text("保存");
        saveHb.getChildren().addAll(saveI, saveT);
        save.setGraphic(saveHb);

        all = new MenuItem();
        allAction();
        HBox allHb = new HBox(10);
        ImageView allI = new ImageView(TAllI);
        allI.setPreserveRatio(true);
        allI.setFitWidth(25);
        Text allT = new Text("全选");
        allHb.getChildren().addAll(allI, allT);
        all.setGraphic(allHb);

        copy = new MenuItem();
        copyAction();
        HBox copyHb = new HBox(10);
        ImageView copyI = new ImageView(TcopyI);
        copyI.setPreserveRatio(true);
        copyI.setFitWidth(25);
        Text copyT = new Text("复制");
        copyHb.getChildren().addAll(copyI, copyT);
        copy.setGraphic(copyHb);

        stickup = new MenuItem();
        stickupAction();
        HBox stickupHb = new HBox(10);
        ImageView stickupI = new ImageView(TstickupI);
        stickupI.setPreserveRatio(true);
        stickupI.setFitWidth(25);
        Text stickypT = new Text("粘贴");
        stickupHb.getChildren().addAll(stickupI, stickypT);
        stickup.setGraphic(stickupHb);

        cut = new MenuItem();
        shearAction();
        HBox shearHb = new HBox(10);
        ImageView shearI = new ImageView(TcutI);
        shearI.setPreserveRatio(true);
        shearI.setFitWidth(25);
        Text shearT = new Text("剪切");
        shearHb.getChildren().addAll(shearI, shearT);
        cut.setGraphic(shearHb);

        quit = new MenuItem();
        quitAction();
        HBox quitHb = new HBox(10);
        ImageView quitI = new ImageView(TquitI);
        quitI.setPreserveRatio(true);
        quitI.setFitWidth(25);
        Text quitT = new Text("退出编辑");
        quitHb.getChildren().addAll(quitI, quitT);
        quit.setGraphic(quitHb);

        areaCm.getItems().addAll(edit, new SeparatorMenuItem(), save, new SeparatorMenuItem(), all
                , new SeparatorMenuItem(), copy, new SeparatorMenuItem(), stickup, new SeparatorMenuItem(),
                cut, new SeparatorMenuItem(),
                quit);
        this.setContextMenu(areaCm);
    }

    private void quitAction() {
        quit.setOnAction(event -> {
            clear();
            appendText(old.toString());
            setEditable(false);
            log.addLog(1, "退出" + status.getPath());
        });
    }

    private void shearAction() {
        cut.setOnAction(event -> {
            if (!this.isEditable()) return;
            cut();
        });
    }

    private void stickupAction() {
        stickup.setOnAction(event -> {
            if (!this.isEditable()) return;
            paste();
        });
    }

    private void copyAction() {
        copy.setOnAction(event -> copy());
    }

    private void allAction() {
        all.setOnAction(event -> selectAll());
    }

    private void savaAction() {
        save.setOnAction(event -> {
            if (!this.isEditable()) return;
            try {
                if (status.isFile() && demo.exists(status.getPath())) {
                    BufferedWriter writer =
                            new BufferedWriter(new OutputStreamWriter(
                                    demo.create(status.getPath(), true)));
                    writer.write(getText());
                    writer.close();
                    setEditable(false);
                    old.delete(0, old.length());
                    old.append(getText());
                    log.addLog(1, "保存" + status.getPath());
                }
            } catch (IOException e) {
                log.addLog(3, "保存" + status.getPath() + "失败.");
                throw new RuntimeException(e);
            }
        });
    }

    private void editAction() {
        edit.setOnAction(event -> {
            setEditable(true);
        });
    }
}
